package com.minmin;

import com.minmin.common.BaseUrl;
import com.minmin.common.Driver;
import com.minmin.common.DriverConfig;
import com.minmin.common.JavaMailUtil;
import com.minmin.testCases.TestProduct;
import com.minmin.testCases.TestPurchase;
import com.minmin.testCases.TestStaff;
import com.minmin.testCases.TestStock;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import org.openqa.selenium.WebDriver;

@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestWeb {
    private static final String baseUrl = new BaseUrl().getBaseUrl();
    //      2:验证环境 3:生产环境 其它:测试环境
    private static final int environment = 1;
    private static final DriverConfig config = getConfig(environment);
    private static WebDriver driver;

    @BeforeAll
    static void login() {
        driver = new Driver(config).getDriver();
    }

    @AfterAll
    static void closeResources() {
//        生产环境发送日志邮件
        if (environment == 3) {
            new JavaMailUtil().sendEmail();
        }
        driver.close();
    }

    public static DriverConfig getConfig(int ver) {
        switch (ver) {
            case 2:
                return new DriverConfig("zdhcs",
                        "123456",
                        "https://yc.lakala.com/b2c-merchant-admin/#/login",
                        "//button/span[text()=\"生产自动化回归店铺(零售版)\"]");
            case 3:
                return new DriverConfig("zdhcs",
                        "123456",
                        "https://yxd.lakala.com/b2c-merchant-admin/#/login",
                        "//button/span[text()=\"生产自动化回归店铺(零售版)\"]");
            default:
                return new DriverConfig("ly1",
                        "123456",
                        "https://fbbc.wsmsd.cn/b2c-merchant-admin/#/login",
                        "//button/span[text()=\"商品同步测试\"]");
        }
    }

    //    @Disabled
    @Order(1)
    @Test
    void testProduct() {
        try {
            log.info("测试相关商品功能");
            new TestProduct(driver).testProduct();
        } catch (Exception e) {
            driver.navigate().refresh();
            throw new RuntimeException(e);
        }
    }

    //        @Disabled
    @Order(2)
    @Test
    void testPurchase() {
        try {
            log.info("测试采购相关功能");
            new TestPurchase(driver).testPurchase();
        } catch (Exception e) {
            driver.navigate().refresh();
            throw new RuntimeException(e);
        }
    }

    @Disabled
    @Order(3)
    @Test
    void testStock() {
        try {
            log.info("测试仓储模块");
            new TestStock(driver).testStock();
        } catch (Exception e) {
            driver.navigate().refresh();
            throw new RuntimeException(e);
        }
    }

    //    @Disabled
    @Order(4)
    @Test
    void testStaff() {
        try {
            log.info("测试人员模块");
            new TestStaff(driver).testStaff();
        } catch (Exception e) {
            driver.navigate().refresh();
            throw new RuntimeException(e);
        }
    }

}
